<?php
error_reporting(E_ALL);

include_once '../common_classes.php';

$candidateList = getAllCandidates();
$userList = getAllVoters();
$keys = array_keys($userList);

echo 'woei';

// total all user fidelities per candidate
/*for($i = 0; $i < count($candidateList); $i++)
{	
	for($j = 0; $j < count($keys); $j++)
	{
		$timekey = array_keys($userList[$keys[$j]]);
		$ip = $keys[$j];
		$time = $timekey[0];
		$user = $userList[$ip][$time];
		
		
		$fidelity = $user->compareWithCandidate($candidateList[$i][0]);

		// total all fidelity scores
		$candidateList[$i][1] = $candidateList[$i][1] + $fidelity;
	}
	
	echo $candidateList[$i][0]->getName() . ', ' . $candidateList[$i][1] . '<br />';	
}*/

for($i = 0; $i < count($keys); $i++)
{	
	$score = 0;
	
	$timekey = array_keys($userList[$keys[$i]]);
	$ip = $keys[$i];
	$time = $timekey[0];
	$user = $userList[$ip][$time];
	
	$old_fidelity = 0;
	$fav_candidate = 0;
	for($j = 0; $j < count($candidateList); $j++)
	{
		$fidelity = $user->compareWithCandidate($candidateList[$j][0]);
		
		if($old_fidelity < $fidelity)
		{
			$old_fidelity = $fidelity;
			$fav_candidate = $j;
		}
	}
	
	$candidateList[$fav_candidate][1] = $candidateList[$fav_candidate][1] + 1; 
}

for($i = 0; $i < count($candidateList); $i++)
{
	echo $candidateList[$i][0]->getName() . ', ' . $candidateList[$i][1] . '<br />';
}

function getAllCandidates() {
	// get the candidate details from the database
	$candidates = QueryBuilder::getAllCandidateDetailsForCurrentElection();
	
	$returnArray;
	
	// add the candidates to the grid
	$ccount = count($candidates);
	$iterator = 0;
	for($i = 0; $i < $ccount; $i++) {
		$answers = QueryBuilder::getAllAnswersForCandidateID($candidates[$i][0]);
	
		// if this candidate has filled in any answers
		if(count($answers) > 0) {
			$candidate = new CandidateInformation();
			$candidate->setID($candidates[$i][0]);
			$candidate->setName($candidates[$i][1]);
			$candidate->setUserID($candidates[$i][2]);
			$candidate->setRealName($candidates[$i][3]);
			$candidate->setDescription($candidates[$i][4]);
			$candidate->setURL($candidates[$i][5]);
			$candidate->setCorp($candidates[$i][6]);
			$candidate->setAlliance($candidates[$i][7]);
			$candidate->setAnswers($answers);
	
			$returnArray[$iterator][0] = $candidate;
			$returnArray[$iterator][1] = 0;
			
			$iterator++;
		}
	}
	
	return $returnArray;
}

function getAllVoters() {
		// make statement
		$query = "SELECT V.voter_ip, V.question_id, V.answer, V.timestamp FROM " . QueryBuilder::votes_table . " AS V WHERE V.election_id = " . Config::active_election . ";";

		// execute it
		$result = queryDB($query);

		// turn the result set into a 2 dimensional array of questions and question ID's
		$users;
		
		$activeUser = "";
		$user = new UserInformation();
		
		while( $row = mysql_fetch_array($result) )
		{
			if($activeUser != $row[0] . $row[3])
			{
				$activeUser = $row[0] . $row[3];
				$user = new UserInformation();
				$users[$row[0]][$row[3]] = $user;
			}

			$users[$row[0]][$row[3]]->setAnswer($row[1], $row[2]);
		}

		return $users;
}


?>